﻿using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.Linq;
using ASoft.Msg;
using System;

namespace ASoft.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class RoomSettingViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the RoomSettingViewModel class.
        /// </summary>
        public RoomSettingViewModel()
        {
            RoomTree = new TreeViewModel();
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                RoomTree.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(RoomTree_PropertyChanged);
                ReLoadTree((x) => false);
            }
        }

        void RoomTree_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == TreeViewModel.FirstSelectedItemPropertyName)
            {
                RaisePropertyChanged("当前温室");
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}

        public TreeViewModel RoomTree { get; private set; }

        private void ReLoadTree(Func<TreeItemViewModel, bool> funcSelected)
        {
            RoomTree.Items.Clear();

            var groups = RC.Current.RootData.温室集合.GroupBy(p => p.RoomGroupName.Trim());
            foreach (var item1 in groups)
            {
                var firstLevel = RoomTree.NewItem();
                firstLevel.Name = item1.Key;
                firstLevel.IsSelected = funcSelected(firstLevel);
                RoomTree.Items.Add(firstLevel);
                foreach (var item2 in item1.OrderBy(p => p.RoomName))
                {
                    var secondLevel = RoomTree.NewItem();

                    var roomVM = new RoomSetBaseViewModel();
                    WPFUtility.TransFromDataToViewModel(item2, roomVM);
                    var setinfo = RC.Current.RootData.温室设置信息集合.Single(p => p.Id == item2.Id);
                    WPFUtility.TransFromDataToViewModel(setinfo, roomVM);
                    secondLevel.Tag = roomVM;

                    firstLevel.Items.Add(secondLevel);
                    secondLevel.IsSelected = funcSelected(secondLevel);

                }
            }
        }

        public RoomSetBaseViewModel 当前温室
        {
            get
            {
                if (RoomTree.FirstSelectedItem == null)
                {
                    return null;
                }
                return RoomTree.FirstSelectedItem.Tag as RoomSetBaseViewModel;
            }
        }


        public ICommand SaveCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        //if (当前温室 == null)
                        //{
                        //    return;
                        //}
                        //var roomData = RC.Current.RootData.温室集合.Single(p => p.Id == this.当前温室.Id);
                        //var roomSetMsg = RC.Current.RootData.温室设置信息集合.Single(p => p.Id == this.当前温室.Id);
                        //WPFUtility.TransFromViewModelToData(this, roomData);
                        //WPFUtility.TransFromViewModelToData(this, roomSetMsg);
                        //RC.Current.SaveRootData();
                    },
                    () =>
                    {
                        return false;
                        //if (IsInDesignMode)
                        //{
                        //    return false;
                        //}
                        //if (当前温室 == null)
                        //{
                        //    return false;
                        //}
                        //var roomData = RC.Current.RootData.温室集合.Single(p => p.Id == this.当前温室.Id);
                        //var roomSetMsg = RC.Current.RootData.温室设置信息集合.Single(p => p.Id == this.当前温室.Id);
                        //return !WPFUtility.DataEqualsViewModel(roomData, this)
                        //    || !WPFUtility.DataEqualsViewModel(roomSetMsg, this);
                    });
            }
        }

        public ICommand AddCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        //int id = 1;
                        //if (RC.Current.RootData.温室集合.Count > 0)
                        //{
                        //    id = RC.Current.RootData.温室集合.Max(p => p.Id) + 1;
                        //}
                        //RoomData roomData = new RoomData() { Id = (byte)id };
                        //RC.Current.RootData.温室集合.Add(roomData);
                        //var roomSetMsg = new RoomSetMsg() { Id = (byte)id };
                        //RC.Current.RootData.温室设置信息集合.Add(roomSetMsg);
                        //RC.Current.SaveRootData();

                        //ReLoadTree((item) =>
                        //{
                        //    if (item.Tag == null)
                        //    {
                        //        return false;
                        //    }
                        //    return (item.Tag as RoomSetBaseViewModel).Id == id;
                        //});
                    });
            }
        }



        public ICommand RemoveCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        //var roomData = RC.Current.RootData.温室集合.Single(p => p.Id == this.当前温室.Id);
                        //var roomSetMsg = RC.Current.RootData.温室设置信息集合.Single(p => p.Id == this.当前温室.Id);
                        //RC.Current.RootData.温室集合.Remove(roomData);
                        //RC.Current.RootData.温室设置信息集合.Remove(roomSetMsg);
                        //ReLoadTree(vm => false);
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        if (当前温室 == null)
                        {
                            return false;
                        }
                        return true;
                    });
            }
        }
    }
}